﻿
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link href="~/layui-v2.6.8/layui-v2.6.8/layui/css/layui.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/layui-v2.6.8/layui-v2.6.8/layui/layui.js"></script>

</head>
<body>

    <form class="layui-form" lay-filter="authree-submit-form">

        <div class="layui-form-item">
            <label class="layui-form-label">选择权限</label>
            <div class="layui-input-block">
                <div id="userId"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" type="submit" lay-submit lay-filter="LAY-auth-tree-submit">提交</button>
                <button class="layui-btn layui-btn-primary" type="reset">重置</button>
            </div>
        </div>
    </form>

    <script>

        //（4） 前端：浏览器请求服务器的时候，要带上JWT
        $.ajaxSetup({
            beforeSend: function (xhr) {
                if (localStorage.getItem("token") !== null) {
                    //（4） 前端：浏览器请求服务器的时候，要带上JWT
                    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("token"));
                }
            }
        });

        layui.config({
            base: '/layui-v2.6.8/',
        }).extend({
            authtree: 'authtree',
        });

        layui.use(['jquery', 'authtree', 'form', 'layer'], function () {

            

            var $ = layui.jquery;
            var authtree = layui.authtree;
            var form = layui.form;
            var layer = layui.layer;
            // 一般来说，权限数据是异步传递过来的
            $.ajax({
                url: 'https://localhost:44398/api/UsersAPI/ShuShow?id='+@ViewBag.id,
                dataType: 'json',
                success: function (data) {
                    console.log(data);

                    var trees = authtree.listConvert(data.data, {
                         startPid: 0
                        ,primaryKey: 'menuID'        //主键
                        , parentKey: 'parentID'     //父级Id
                        , nameKey: 'menuName'       //节点标题对应Key
                        , valueKey: 'menuID'        //节点值对应Key
                        , checkedKey: data.checkedId        //节点是否选中
                    });

                    // 如果后台返回的不是树结构，请使用 authtree.listConvert 转换
                    console.log(trees);
                    authtree.render('#userId', trees, {
                        inputname: 'authids[]',
                        layfilter: 'lay-check-auth',
                        autowidth: true,
                    });
                }
            });
            form.on('submit(LAY-auth-tree-submit)', function () {
                var Checked = authtree.getChecked('#userId');
                var data = {
                    menuIds: Checked, userId: @ViewBag.id,
                }
                $.ajax({
                    url: 'https://localhost:44398/api/UsersAPI/AddRoleMenus',
                    dataType: 'json',
                    type: 'post',
                    data: data,
                    success: function (d) {
                        console.log(d);
                        if (d > 0) {
                            layer.msg("分配成功", { time: 2000, icon: 1 }, function () {
                                parent.location.reload();
                            })
                        } else {
                            layer.msg("分配失败", { time: 2000, icon: 1 }, function () {
                                parent.location.reload();
                            })
                        }
                    }
                });
                return false;
            });
        });
    </script>

</body>
</html>